package com.zzq.controller;

import com.zzq.constant.MessageConstant;
import com.zzq.constant.RedisConstant;
import com.zzq.constant.RedisMessageConstant;
import com.zzq.entity.Result;
import com.zzq.utils.SMSUtils;
import com.zzq.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 幼儿猿大班
 * 2021/7/27
 */
@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/sendcode")
    public Result sendcode(String telephone){
        System.out.println(telephone);
        Jedis jedis =null;
        try {
            String s = ValidateCodeUtils.generateValidateCode4String(6);
            SMSUtils.sendShortMessage("18852895380",s);
            jedis = jedisPool.getResource();
            jedis.set(telephone+ RedisMessageConstant.SENDTYPE_ORDER,s);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
           return  new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }finally {
            if(jedis!=null){
                jedis.close();
            }
        }
    }
}
